package com.eerl.pdev;

import com.intellij.openapi.options.Configurable;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class MyConfigurable implements Configurable {

    private final int INPUTS_NUMBER = 5;
    private final JTextField debugDefineTextField = new JTextField(20);
    private final JTextField excludeEVarPatternTextField = new JTextField(40);
    private final JTextField includeEVarPatternTextField = new JTextField(40);
    // 节点完整名
    private final JTextField erlNodeNameTextField = new JTextField(40);
    // erl 通信cookie
    private final JTextField erlCookieTextField = new JTextField(40);

    // 项目的src文件绝对路径(别问为什么不直接项目根路径，因为不好起名字，容易和event.getProject的重复了)
    private final JTextField projectSrcAbsPath = new JTextField(40);

    // 项目内的 tools 文件绝对路径
    private final JTextField projectToolsAbsPath = new JTextField(40);
    //模板根路径
    private final JTextField genTemplateRootAbsPath = new JTextField(40);
    // 是否处于debug状态
    private final JCheckBox isDebugSelectField = new JCheckBox();
    // 项目本地data配置路径
    private final JTextField localErlDataConfigAbsPathField = new JTextField(60);
    // 项目开发data配置路径
    private final JTextField devErlDataConfigAbsPathField = new JTextField(60);
    // wps et.exe 执行路径
    private final JTextField wpsEtExeAbsPathField = new JTextField(60);

    private final JPanel jPanel;

    public MyConfigurable() {

        JLabel[] jLabels = getJLabels();

        JTextField[] inputs = {
            debugDefineTextField, excludeEVarPatternTextField, includeEVarPatternTextField,
            projectSrcAbsPath, projectToolsAbsPath, genTemplateRootAbsPath, localErlDataConfigAbsPathField,
            devErlDataConfigAbsPathField, wpsEtExeAbsPathField, erlNodeNameTextField, erlCookieTextField
        };
        // 屏蔽，先不开发这个
        projectSrcAbsPath.setEnabled(false);
        projectToolsAbsPath.setEnabled(false);
        genTemplateRootAbsPath.setEnabled(false);

        jPanel = new JPanel();
        BoxLayout boxLayout = new BoxLayout(jPanel, BoxLayout.Y_AXIS);
        jPanel.setLayout(boxLayout);

        for (int i = 0; i < jLabels.length; i++) {
            // sb 字体
            jLabels[i].setFont(JBUI.Fonts.create("Microsoft YaHei, JetBrains Mono", 14));
            JPanel innerJPanel = new JPanel();
            innerJPanel.setMaximumSize(new Dimension(1000, 50));
            FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
            innerJPanel.setLayout(layout);
            innerJPanel.add(jLabels[i]);
            innerJPanel.add(inputs[i]);
            jPanel.add(innerJPanel);
        }
        // select box
        JLabel isDebugLabelText = new JLabel("IsDebug");
        isDebugLabelText.setFont(JBUI.Fonts.create("Microsoft YaHei, JetBrains Mono", 14));
        JPanel innerJPanel = new JPanel();
        innerJPanel.setMaximumSize(new Dimension(1000, 50));
        FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
        innerJPanel.setLayout(layout);
        innerJPanel.add(isDebugLabelText);
        innerJPanel.add(isDebugSelectField);
        jPanel.add(innerJPanel);

    }

    // input text fields
    @NotNull
    private static JLabel[] getJLabels() {
        JLabel debugDefineText = new JLabel("变量打印宏定义:", JLabel.LEFT);
        JLabel excludeEVarPattern = new JLabel("变量过滤正则式:", JLabel.LEFT);
        JLabel includeEVarPattern = new JLabel("变量反过滤正则:", JLabel.LEFT);
        // md sb 死都对不齐, cao啊~~~~~~~
        JLabel projectSrcAbsText      = new JLabel("项目_src文件绝对路径", JLabel.LEFT);
        JLabel projectToolsAbsText    = new JLabel("项目tool文件绝对路径", JLabel.LEFT);
        JLabel genTemplateRootAbsText = new JLabel("模板temp文件绝对路径", JLabel.LEFT);
        JLabel localErlDataConfigAbsPathText = new JLabel("项目本地data文件绝对路径", JLabel.LEFT);
        JLabel devErlDataConfigAbsPathText = new JLabel("项目开发data文件绝对路径", JLabel.LEFT);
        JLabel wpsEtExePathText = new JLabel("wps et可执行文件路径", JLabel.LEFT);

        JLabel erlNodeNameText = new JLabel("Erl 节点全名:", JLabel.LEFT);
        JLabel erlCookieText = new JLabel("Erl 节点Cookie:", JLabel.LEFT);

        return new JLabel[]{
            debugDefineText, excludeEVarPattern, includeEVarPattern, projectSrcAbsText, projectToolsAbsText,
            genTemplateRootAbsText, localErlDataConfigAbsPathText, devErlDataConfigAbsPathText, wpsEtExePathText,
            erlNodeNameText, erlCookieText
        };
    }

    @NotNull
    public String getId() {
        return "com.myplugin";
    }

    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return "My Plugin Settings";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        // let persistence state to init setting
        debugDefineTextField.setText(MySettingsState.getInstance().myValue);
        excludeEVarPatternTextField.setText(MySettingsState.getInstance().EXCLUDE_VAR_FILTER_PATTERN);
        includeEVarPatternTextField.setText(MySettingsState.getInstance().INCLUDE_VAR_FILTER_PATTERN);
        projectSrcAbsPath.setText(MySettingsState.getInstance().PROJECT_ERL_SRC_ABS_PATH);
        projectToolsAbsPath.setText(MySettingsState.getInstance().PROJECT_ERL_TOOLS_ABS_PATH);
        genTemplateRootAbsPath.setText(MySettingsState.getInstance().GEN_TEMPLATE_ABS_ROOT_PATH);
        localErlDataConfigAbsPathField.setText(MySettingsState.getInstance().LOCAL_ERL_DATA_CONFIG_ABS_PATH);
        devErlDataConfigAbsPathField.setText(MySettingsState.getInstance().DEV_ERL_DATA_CONFIG_ABS_PATH);
        wpsEtExeAbsPathField.setText(MySettingsState.getInstance().WPS_ET_EXE_ABS_PATH);
        isDebugSelectField.setSelected(MySettingsState.getInstance().Is_DEBUG);
        erlNodeNameTextField.setText(MySettingsState.getInstance().ERL_NODE_NAME);
        erlCookieTextField.setText(MySettingsState.getInstance().ERL_NODE_COOKIE);
        return jPanel;
    }

    @Override
    public boolean isModified() {
        return true;
    }

    @Override
    public void apply() {
        // let setting to persistence
        MySettingsState.getInstance().myValue = debugDefineTextField.getText();
        MySettingsState.getInstance().EXCLUDE_VAR_FILTER_PATTERN = excludeEVarPatternTextField.getText();
        MySettingsState.getInstance().INCLUDE_VAR_FILTER_PATTERN = includeEVarPatternTextField.getText();
        MySettingsState.getInstance().PROJECT_ERL_SRC_ABS_PATH = projectSrcAbsPath.getText();
        MySettingsState.getInstance().PROJECT_ERL_TOOLS_ABS_PATH = projectToolsAbsPath.getText();
        MySettingsState.getInstance().GEN_TEMPLATE_ABS_ROOT_PATH = genTemplateRootAbsPath.getText();
        MySettingsState.getInstance().LOCAL_ERL_DATA_CONFIG_ABS_PATH = localErlDataConfigAbsPathField.getText();
        MySettingsState.getInstance().DEV_ERL_DATA_CONFIG_ABS_PATH = devErlDataConfigAbsPathField.getText();
        MySettingsState.getInstance().WPS_ET_EXE_ABS_PATH = wpsEtExeAbsPathField.getText();
        MySettingsState.getInstance().Is_DEBUG = isDebugSelectField.isSelected();
        MySettingsState.getInstance().ERL_NODE_NAME = erlNodeNameTextField.getText();
        MySettingsState.getInstance().ERL_NODE_COOKIE = erlCookieTextField.getText();
    }

    // ... other necessary overrides
}